home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / comm / bbs / cit_src_AD08.lha / file_comments.c < prev    next >
C/C++ Source or Header  |  1998-04-28  |  9KB  |  279 lines

  1. /************************************************************************/
  2. /*   File Comments Processing                                           */
  3. /************************************************************************/
  4. #define SYSTEM_DEPENDENT
  5. #define TIMER_FUNCTIONS_NEEDED
  6. #include "ctdl.h"
  7. #include "dos.h"
  8. #include "time.h"
  9. #include "string.h"
  10. #include "stdarg.h"
  11. #include "stat.h"
  12.  
  13. /************************************************************************/
  14. /*      Globals -- there shouldn't be anything here but statics and     */
  15. /*      externs.                                                        */
  16. /************************************************************************/
  17. static char *Phrase;
  18. extern char *READ_TEXT;
  19. extern char *WRITE_TEXT;
  20. static FILE *FileTag;                  /* For the file tags            */
  21. #define ONE_DAY         86400L
  22. #define D_OFFSET        252460800L
  23.  
  24. int Jsystem(char *);
  25. void Do_Stack_Check(void);
  26.  
  27.  
  28. extern MessageBuffer   msgBuf;
  29. extern CONFIG    cfg;            /* Lots an lots of variables    */
  30. void *ChPhrase(DirEntry *e, DirEntry *d);
  31. static char  File_Extension(char *name, char *extension);
  32. static void Change_Extension(char *name, char *extension);
  33. static char *Find_Extension(char *name);
  34.  
  35. static char filenamebuffer[100];
  36.  
  37.  
  38. #define ONE_DAY         86400L
  39. #define D_OFFSET        252460800L
  40.  
  41. static char *Find_Extension(char *name)
  42.   {
  43.   char *ptr, *lptr;
  44.   ptr = name;  /* point to last character */
  45.   while( ( lptr = strchr(ptr,'.') ) != NULL )
  46.     {
  47.     ptr = lptr;
  48.     if( *ptr == '.' ) ptr++;
  49.     };
  50.   return ptr;
  51.   }
  52.  
  53. static void Change_Extension(char *name, char *extension)
  54.   {
  55.   char *ptr;
  56.   int lfn, lex;
  57.   /**
  58.     Find the Extension of the name
  59.   **/
  60.   lfn  = strlen(name);
  61.   ptr  = Find_Extension(name);
  62.   lex  = strlen(ptr);
  63.   if( lfn > lex )lfn -= lex;  /* length of filename without extention */
  64.   memset(filenamebuffer,'\0',100);
  65.   strncpy(filenamebuffer,name,lfn);
  66.   strcat(filenamebuffer,extension);
  67.   return;
  68.   }
  69.  
  70. static char  File_Extension(char *name, char *extension)
  71.   {
  72.   char *ptr;
  73.   int lnm, lext;
  74.   /**
  75.     1) check to see that we have a valid pointer and that
  76.        the string is long enough to match
  77.   **/
  78.   if( name == NULL ) return (char)TRUE;
  79.   lnm =  strlen(name);
  80.   lext=  strlen(extension);
  81.   if( lnm < lext ) return (char)TRUE;
  82.   /**
  83.     Find the Extension of the name
  84.   **/
  85.   ptr = Find_Extension(name);
  86.   lnm = strlen(ptr);
  87.   if( lnm != lext ) return (char)FALSE;
  88.   if( stricmp(ptr, extension) == 0 ) return (char)TRUE;
  89.   return (char)FALSE;
  90.   }
  91.  
  92. /************************************************************************/
  93. /*      CitGetFileList() Get a list of files from the current "area"    */
  94. /************************************************************************/
  95. void CitGetFileList(char *mask, SListBase *list, long before, long after,
  96. char *phrase)
  97.   {
  98.   struct FileInfoBlock *FlBlock;
  99.   extern char     *monthTab[13];
  100.   char            *w, *work, *sp, again, buf[10];
  101.   DirEntry        *fp;
  102.   int             done;
  103.   long            time;
  104.   struct tm         *v;
  105.   Do_Stack_Check();
  106.   w = work = strdup(mask);
  107.   FlBlock = malloc(sizeof *FlBlock);
  108.   do
  109.     {
  110.     again = (sp = strchr(work, ' ')) != NULL;      /* space is separator */
  111.     if (again)
  112.       {
  113.       *sp = 0;
  114.  
  115.       }
  116.     /* Do all scanning for illegal requests here */
  117.     if (!ValidDirFileName(work))    continue;
  118.     for (done = dfind(FlBlock, work, 0);  !done;    done = dnext(FlBlock))
  119.       {
  120.       if( File_Extension(FlBlock->fib_FileName, "readme") )
  121.         continue; /* skip all readme files */
  122.       /* format date to our standards */
  123.       /* DosToNormal(buf, FlBlock->ff_fdate);  */
  124.       time = D_OFFSET + (long)(ONE_DAY * FlBlock->fib_Date.ds_Days) +
  125.       (long)(FlBlock->fib_Date.ds_Minute * 60) +
  126.       (long)(FlBlock->fib_Date.ds_Tick / 50);
  127.       v = localtime(&time);
  128.       sPrintf(buf, "%d%s%02d", v->tm_year, monthTab[v->tm_mon+1],
  129.       v->tm_mday);
  130.       /* now read it so we can handle date specs */
  131.       ReadDate(buf, &time);
  132.       /* add to list iff dates inactive or we meet the specs */
  133.       if ((before != -1l && time > before) || (after  != -1l && time < after))
  134.         continue;
  135.       fp = (DirEntry *) GetDynamic(sizeof *fp);
  136.       fp->unambig = strdup(FlBlock->fib_FileName);
  137.       strCpy(fp->FileDate, buf);
  138.       fp->FileSize = FlBlock->fib_Size;
  139.       AddData(list, fp, NULL, TRUE);
  140.  
  141.       }
  142.     if (again) work = sp+1;
  143.  
  144.     }
  145.   while (again);
  146.   free(w);
  147.   free(FlBlock);
  148.   if (strLen(phrase) != 0)
  149.     {
  150.     Phrase = phrase;
  151.     StFileComSearch();      /* so's we can do phrase searches */
  152.     list->CheckIt = ChPhrase;   /* COVER YOUR EYES I'M CHEATING! */
  153.     KillData(list, list);
  154.     list->CheckIt = DirCheck;   /* COVER YOUR EYES I'M CHEATING! */
  155.     EndFileComment();
  156.  
  157.     }
  158.  
  159.   }
  160.  
  161. /************************************************************************/
  162. /*      ChPhrase() does something to do with finding phrases in comments*/
  163. /************************************************************************/
  164. void *ChPhrase(DirEntry *e, DirEntry *d)
  165.   {
  166.   Do_Stack_Check();
  167.   if (FindFileComment(e->unambig))
  168.     {
  169.     if (matchString(msgBuf.mbtext, Phrase, lbyte(msgBuf.mbtext)) == NULL) return d;
  170.  
  171.     }
  172.   else return d;
  173.   return NULL;
  174.  
  175.   }
  176.  
  177. /************************************************************************/
  178. /*      CopyFile() copies a file                                        */
  179. /************************************************************************/
  180. char CopyFile(char *oldname, aRoom *roomData)
  181.   {
  182.   int len;
  183.   struct stat *buf;
  184.   char *temp;
  185.   char buffer[256];
  186.   char F_Node[32],F_Ext[32];
  187.   Do_Stack_Check();
  188.   if ((temp = FindDirName(roomData->rbArea)) == NULL)
  189.     {
  190.     Output_Citadel_Message("ERRDRM",NULL,NULL,NULL);
  191.     return FALSE;
  192.  
  193.     }
  194.   buf = (struct stat *)calloc( sizeof(struct stat), 1);
  195.   if( buf == NULL )
  196.     {
  197.     Output_Citadel_Message("ERRDRP",(long) oldname,NULL,NULL);
  198.     return FALSE;
  199.  
  200.     }
  201.   if (stat(oldname, buf) != 0 || !(buf->st_mode & S_IFREG))
  202.     {
  203.     Output_Citadel_Message("ISNCPY",(long) oldname,NULL,NULL);
  204.     free(buf);
  205.     return FALSE;
  206.     };
  207.   free(buf);
  208.   sPrintf(buffer, "copy >NIL: %s %s", oldname, temp);
  209.   Jsystem(buffer);  /* move file */
  210.   Change_Extension(oldname,"readme");
  211.   if( access(filenamebuffer,R_OK) == 0 )
  212.     {
  213.     sPrintf(buffer, "copy >NIL: %s %s", filenamebuffer, temp);
  214.     Jsystem(buffer);  /* move readme */
  215.     ZeroMsgBuffer(&msgBuf);
  216.     ingestFile(filenamebuffer,&msgBuf);
  217.     };
  218.   strsfn(oldname,NULL,NULL,F_Node,F_Ext);
  219.   temp = FindDirName(roomData->rbArea);
  220.   sPrintf(oldname, "%s%c%s", F_Node,(F_Ext[0] != 0) ? '.' : '\0', F_Ext);
  221.   len = strlen(temp);
  222.   if ( temp[len-1] == ':' || temp[len-1] == '/' )
  223.     {
  224.     sPrintf(buffer, "%s%s",temp,oldname);
  225.     }
  226.   else    sPrintf(buffer, "%s/%s",temp,oldname);
  227.   return ((char)(access(buffer, 0) == 0));
  228.  
  229.   }
  230.  
  231. /************************************************************************/
  232. /*      StFileComSearch() Start File Comment search: see SYSDEP.DOC.    */
  233. /************************************************************************/
  234. int StFileComSearch()
  235.   {
  236.   return TRUE;  /* a stub, nothing to do  */
  237.   }
  238.  
  239. /************************************************************************/
  240. /*      FindFileComment() find file comment for file: see SYSDEP.DOC.   */
  241. /************************************************************************/
  242. int FindFileComment(char *fileName)
  243.   {
  244.   char Found_Comment;
  245.   Do_Stack_Check();
  246.   ZeroMsgBuffer(&msgBuf);
  247.   Change_Extension(fileName,"readme");
  248.   Found_Comment = ingestFile(filenamebuffer,&msgBuf);
  249.   doCR();               /* formatting without blank lines.              */
  250.   return ( Found_Comment );
  251.   }
  252.  
  253. /************************************************************************/
  254. /*      EndFileComment() end session of reading file comments.          */
  255. /************************************************************************/
  256. void EndFileComment()
  257.   {
  258.   return;   /* just a stub */
  259.   }
  260.  
  261. /************************************************************************/
  262. /*      updFiletag()  updates a file tag                                */
  263. /************************************************************************/
  264. void updFiletag(char *fileName, char *desc)
  265.   {
  266.   Do_Stack_Check();
  267.   Change_Extension(fileName, "readme");
  268.   if ((FileTag = safeopen(filenamebuffer, WRITE_TEXT)) == NULL)
  269.     {
  270.     Output_Citadel_Message("UNKWNP",(long)filenamebuffer,NULL,NULL);
  271.     }
  272.   else
  273.     {
  274.     fprintf(FileTag, "%s\n",  desc);
  275.     fclose( FileTag);
  276.     }
  277.   return;
  278.   }
  279.